package com.weirdor.wxchat.facade.account;

import com.weirdor.wxchat.facade.user.TagAPI;
import com.weirdor.wxchat.support.exception.WexinReqException;
import com.weirdor.wxchat.support.httpClient.HttpsClient;
import com.weirdor.wxchat.support.utils.JsonUtils;
import com.xiaoleilu.hutool.http.HttpUtil;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by weirdor on 2017/11/28.
 */
public class AccountAPI {

    private static Logger logger = LoggerFactory.getLogger(AccountAPI.class);


    /**
     * 二维码类型，QR_SCENE为临时, QR_LIMIT_SCENE为永久, QR_LIMIT_STR_SCENE为永久的字符串参数值
     */
    public static final String QRCODE_TYPE_SCENE = "QR_SCENE";
    public static final String QRCODE_TYPE_LIMIT = "QR_LIMIT_SCENE";
    public static final String QRCODE_TYPE_LIMIT_STR = "QR_LIMIT_STR_SCENE";

    /**
     * 代表长链接转短链接
     */
    public static final String SHORT_URL_ACTION = "long2short";



    private static String create_short_url = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=ACCESS_TOKEN";

    private static String create_qrcode_url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=ACCESS_TOKEN";


    /**
     *
     * expire_seconds	该二维码有效时间，以秒为单位。 最大不超过1800。
     action_name	二维码类型，QR_SCENE为临时,QR_LIMIT_SCENE为永久,QR_LIMIT_STR_SCENE为永久的字符串参数值
     action_info	二维码详细信息
     scene_id	场景值ID，临时二维码时为32位非0整型，永久二维码时最大值为100000（目前参数只支持1--100000）
     scene_str	场景值ID（字符串形式的ID），字符串类型，长度限制为1到64，仅永久二维码支持此字段
     * @param accessToken
     * @param scene_str
     * @param action_name
     * @param expire_seconds
     * @return
     * @throws WexinReqException
     */
    public static String createQrcode(String accessToken, String scene_str,
                                        String action_name, String expire_seconds) throws WexinReqException {
        JSONObject result = null;
        String requestUrl = create_qrcode_url.replace("ACCESS_TOKEN", accessToken);
        Map<String,Object> map=new HashMap<>();
        map.put("expire_seconds",expire_seconds);
        map.put("action_name",action_name);
        Map<String,Object> info=new HashMap<>();
        Map<String,Object> scene=new HashMap<>();
        scene.put("scene_str",scene_str);
        info.put("scene",scene);
        map.put("action_info",info);
        String response= HttpUtil.post(requestUrl, JsonUtils.to(map));
        result = JSONObject.fromObject(response);
        Object error = result.get("ticket");
        String ticket = "";
        if (error != null) {
            ticket = result.getString("ticket");
        }else{
            ticket = result.getString("errmsg");
        }
        return ticket;
    }

    /**
     * 获取短链接信息
     * @param accessToken
     * @param longUrl
     *       需要转换的长链接，支持http://、https://、weixin://wxpay 格式的url
     * @return
     * @throws WexinReqException
     */
    public static String getShortUrl(String accessToken,String longUrl) throws WexinReqException{
        JSONObject result = null;
        String requestUrl = create_short_url.replace("ACCESS_TOKEN", accessToken);
        Map<String ,Object> map=new HashMap<>();
        map.put("access_token",accessToken);
        map.put("long_url",longUrl);
        map.put("action",SHORT_URL_ACTION);
        logger.info("编辑标签方法执行前json参数---obj: "+map.toString());
        String response= HttpUtil.post(requestUrl, JsonUtils.to(map));
        result = JSONObject.fromObject(response);
        Object error = result.get("short_url");
        String shortUrl = "";
        if (error != null) {
            shortUrl = result.getString("short_url");
        }else{
            shortUrl = result.getString("errmsg");
        }
        return shortUrl;
    }

    public static void main(String[] args) throws WexinReqException {
        System.out.println(createQrcode("DHmd3qFzxjTALsqmTVTNOU4qmbUXqxiBlPEaGlts5SKaS1Nt0oHWQt8y7mb7JqLCtwxMAE_Q883YlkR8S12JiGYobNyFE-rdVTQSdXjEo0YKSCiAIABJT","test",QRCODE_TYPE_LIMIT,"604800"));    }


}
